import { Injectable } from '@angular/core';
import {RequestOptions,Http} from "@angular/http";
import {ConnectionBackend,XHRConnection,XHRBackend,Request} from "@angular/http";
import {HttpInterceptorService} from "./http-interceptor.service";

@Injectable()
 class HttpInterceptorBackendService implements ConnectionBackend{

  constructor(private _hip:HttpInterceptorService,private _xhrb:XHRBackend) {
    console.log('HttpInterceptorBackendService')
  }
  createConnection(request:Request):XHRConnection{
    let inter=this._hip;
    let req=inter.beforeRequest?inter.beforeRequest(request):request;
    let result=this._xhrb.createConnection(req);
    result.response=inter.afterResponse?inter.afterResponse(result.response):result.response;
    return result;
  }
}


function httpFactory(hib:HttpInterceptorBackendService,reqo:RequestOptions):Http{
    console.log('httpFactory')
    return new Http(hib,reqo);
}
export {HttpInterceptorBackendService,httpFactory}
